@echo off
chcp 65001 >nul
setlocal enabledelayedexpansion

:: PPH系统依赖安装脚本
:: 必须在Node.js 18.17.1环境下运行

echo ========================================
echo PPH Dependencies Installation
echo ========================================
echo.

set "DEPLOY_DIR=%~dp0.."
set "BACKEND_DIR=%DEPLOY_DIR%\app\backend"
set "MANAGEMENT_DIR=%DEPLOY_DIR%\management"

echo [INFO] Deploy directory: %DEPLOY_DIR%
echo.

:: 检查管理员权限
net session >nul 2>&1
if %errorlevel% neq 0 (
    echo [ERROR] Administrator privileges required
    echo Please run this script as Administrator
    pause
    exit /b 1
)
echo [OK] Administrator privileges confirmed

:: 严格检查Node.js版本
echo [INFO] Checking Node.js version...
node --version >nul 2>&1
if %errorlevel% neq 0 (
    echo [ERROR] Node.js not found
    echo Please install Node.js 18.17.1 from:
    echo https://nodejs.org/dist/v18.17.1/node-v18.17.1-x64.msi
    pause
    exit /b 1
)

for /f "tokens=*" %%i in ('node --version') do set NODE_VERSION=%%i
echo Current Node.js version: %NODE_VERSION%

:: 严格检查版本必须为18.17.1
echo %NODE_VERSION% | findstr "v18\.17\.1" >nul 2>&1
if %errorlevel% neq 0 (
    echo [ERROR] Node.js 18.17.1 is required!
    echo Current version: %NODE_VERSION%
    echo.
    echo ❌ Forbidden versions: 22.x, 20.x, 19.x (will cause dependency conflicts)
    echo ✅ Required version: 18.17.1 (exact version)
    echo.
    echo Please install the correct version:
    echo   1. Download: https://nodejs.org/dist/v18.17.1/node-v18.17.1-x64.msi
    echo   2. Or use nvm: nvm install 18.17.1 && nvm use 18.17.1
    echo.
    pause
    exit /b 1
)
echo [OK] Node.js 18.17.1 confirmed - correct version!
echo.

:: 安装全局依赖
echo [STEP 1] Installing Global Dependencies
echo ========================================
echo [INFO] Installing PM2 process manager...
npm install -g pm2
if %errorlevel% neq 0 (
    echo [ERROR] Failed to install PM2
    pause
    exit /b 1
)
echo [OK] PM2 installed successfully
echo.

:: 清理旧依赖（如果存在）
echo [STEP 2] Cleaning Old Dependencies
echo ========================================
echo [INFO] Cleaning old node_modules...

if exist "%BACKEND_DIR%\node_modules" (
    echo [INFO] Removing backend node_modules...
    rmdir /s /q "%BACKEND_DIR%\node_modules"
)

if exist "%MANAGEMENT_DIR%\node_modules" (
    echo [INFO] Removing management node_modules...
    rmdir /s /q "%MANAGEMENT_DIR%\node_modules"
)
echo [OK] Old dependencies cleaned
echo.

:: 安装后端依赖
echo [STEP 3] Installing Backend Dependencies
echo ========================================
cd /d "%BACKEND_DIR%"
echo [INFO] Installing backend dependencies in 18.17.1 environment...
echo Current directory: %CD%
echo Node.js version:
node --version

npm install --production --no-optional
if %errorlevel% neq 0 (
    echo [ERROR] Failed to install backend dependencies
    pause
    exit /b 1
)
echo [OK] Backend dependencies installed successfully
echo.

:: 安装管理界面依赖
echo [STEP 4] Installing Management Dependencies
echo ========================================
cd /d "%MANAGEMENT_DIR%"
echo [INFO] Installing management dependencies in 18.17.1 environment...
echo Current directory: %CD%
echo Node.js version:
node --version

npm install --production --no-optional
if %errorlevel% neq 0 (
    echo [ERROR] Failed to install management dependencies
    pause
    exit /b 1
)
echo [OK] Management dependencies installed successfully
echo.

:: 验证关键依赖
echo [STEP 5] Verifying Key Dependencies
echo ========================================
cd /d "%BACKEND_DIR%"
echo [INFO] Verifying sqlite3 module...
npm list sqlite3
if %errorlevel% neq 0 (
    echo [ERROR] sqlite3 module not found or incompatible
    pause
    exit /b 1
)
echo [OK] sqlite3 module verified
echo.

echo ========================================
echo Dependencies Installation Complete
echo ========================================
echo.
echo ✅ All dependencies installed successfully in Node.js 18.17.1 environment
echo ✅ PM2 process manager ready
echo ✅ Backend dependencies ready
echo ✅ Management dependencies ready
echo ✅ sqlite3 module verified
echo.
echo Next step: Run start-services.bat to start the system
echo.
pause